var localObj = window.location;
var basePath = localObj.protocol+"//"+localObj.host;
var modelAddModule=angular.module('userDistrictAddApp', []);
    modelAddModule.controller('userDistrictAddCtr',['$scope', '$http',function($scope, $http) {
		
		
		// 点击保存按钮，新增一条数据
		$scope.saveUserDistrict = function() {
			// form表单的验证 数据不对，return false
				if (!$scope.userId) {
					toastr.warning("用户名称不可为空！");
					return false;
				}
				else if ($("#province").val() == '') {
					toastr.warning("版本不可为空！");
					return false;
				}
				submitData(); 
		};

		
		// form表单提交方法 submit(若是校验都通过啦，数据便可以提交啦)
		function submitData() {
			
			$http({
				url : basePath+'/user/saveUserDistrict',
				method : 'POST',
				params : {
					'userId'  : $scope.userId,
					'userName':$("#userId").find("option:selected").text(),
					'province'   :  $scope.province,
					'provinceName':$("#province").find("option:selected").text(),
					'city' : $scope.city,
					'cityName':$("#city").find("option:selected").text(),
					'county' : $scope.county,
					'countyName':$("#county").find("option:selected").text()
				}
			}).success(function(data, header, config, status) {
				toastr.success("操作成功");
				setTimeout(function() {
					location.href = basePath+'/user/userDistrict';
				}, 500);
			}).error(function(data, header, config, status) {
				toastr.error("调用错误！");
				setTimeout(function() {
					//location.href = basePath+'/system/sysUser/user_apply.jsp';
				}, 500);
			});
		}
		//  点击取消按钮，跳转到查询页面
		$scope.backUser = function() {
			location.href = basePath+'/user/userDistrict';
		};
}]);
    $(document).ready(function() { 
    	
    $('#province').change(function(){ 
		var districtCode=$(this).children('option:selected').val(); 
		$.ajax({
			type : "post",
			dataType : "json",
			url : basePath+"/district/district",
			data : {parentCode:districtCode},
			success : function(data) {
				if(data.length>0){
					$("#city").empty();
					$("#city").append("<option value=''>请选择</option>");
					$("#county").empty();
					$("#county").append("<option value=''>请选择</option>");
					for(var i=0;i<data.length;i++){
						$("#city").append("<option value="+data[i].code+">"+data[i].name+"</option>");
					}
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				
			}
		 }); 
	}) 
	
	$('#city').change(function(){ 
		var districtCode=$(this).children('option:selected').val(); 
		$.ajax({
			type : "post",
			dataType : "json",
			url : basePath+"/district/district",
			data : {parentCode:districtCode},
			success : function(data) {
				if(data.length>0){
					$("#county").empty();
					$("#county").append("<option value=''>请选择</option>");
					for(var i=0;i<data.length;i++){
						$("#county").append("<option value="+data[i].code+">"+data[i].name+"</option>");
					}
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				
			}
		 }); 
	}) 
    });